org	07c00h

BaseOfStack		equ	07c00h

BaseOfLoader	equ	09000h
OffsetOfLoader	equ	0100h		;place set loader

jmp short LABEL_START
nop

%include "fat12hdr.inc"

BS_OEMName		DB	'miucsfat'	; 8 bytes
BPB_BytsPerSec	DW	512
BPB_SecPerClus	DB	1
BPB_RsvdSecCnt	DW	1
BPB_NumFATs		DB	2
BPB_RootEntCnt	DW	224
BPB_TotSec16	DW	2880
BPB_Media		DB	0xF0
BPB_FATSz16		DW	9
BPB_SecPerTrk	DW	18
BPB_NumHeads	DW	2
BPB_HiddSec		DD	0
BPB_TotSec32	DD	0
BS_DrvNum		DB	0
BS_Reserved1	DB	0
BS_BootSig		DB	29h
BS_VolID		DD	0
BS_VolLab		DB	' Myos 0.05 '	; 11 bytes
BS_FileSysType	DB	'FAT12   '		; 8  bytes

LABEL_START:
;	mov		ax, cs
;	mov		ds, ax
;	mov		es, ax
;	mov		dh, 0
;	Call	PrintStr
;						black history
;	start load loader

	mov ax, cs
	mov ds, ax
	mov es, ax
	mov ss, ax

	mov dh, 0
	call PrintStr

	mov sp, BaseOfStack
	
	xor ah, ah
	xor dl, dl
	int 13h			; reset disk

	; wSectorNo = SectorNoOfRootDirectry

LABEL_SEARCH_IN_ROOT_DIR_BEGIN:
	
	cmp word [wRootDirSizeForLoop], 0
	jz	LABEL_NO_LOADERBIN
	dec word [wRootDirSizeForLoop]
	mov ax, BaseOfLoader
	mov	es, ax
	mov bx, OffsetOfLoader			; set place to write
	mov ax, [wSectorNo]				; read Sector begin in ax
	mov	cl, 1						; and read cl times
	call ReadSector

	mov si, LoaderFileName			; get file name in	ds:si
	mov di, OffsetOfLoader			; reset				es:di begin of writed thing

	cld								; 0 to DF

	mov dx, 10h						; hey carefull is 16 = 512 / 32

LABEL_SEARCH_FOR_LOADERBIN:
	cmp dx, 0
	jz LABEL_GOTO_NEXT_SECTOR_IN_ROOT_DIR
	dec dx
	mov cx, 11						; set filename Length = 11... FAT12..

LABEL_CMP_FILENAME:
	cmp cx, 0
	jz	LABEL_FILENAME_FOUND		; you get it
	dec cx
	lodsb							; ds:si -> al , inc si

	cmp al, byte [es:di]
	jz	LABEL_GO_ON
	jmp LABEL_DIFFERENT

LABEL_GO_ON:
	inc di
	jmp LABEL_CMP_FILENAME

LABEL_DIFFERENT:
	and di, 0FFE0h					; reset now file index
	add di, 20h						; next Sector
	mov si, LoaderFileName			; reset si

	jmp LABEL_SEARCH_FOR_LOADERBIN	; just jmp you see

LABEL_GOTO_NEXT_SECTOR_IN_ROOT_DIR:
	add word [wSectorNo], 1			; next Sector	
	jmp LABEL_SEARCH_IN_ROOT_DIR_BEGIN

LABEL_NO_LOADERBIN:
	mov dh, 2
	call PrintStr
	jmp $

LABEL_FILENAME_FOUND:
	mov ax, RootDirSectors
	and di, 0FFE0h					; reset now Sectors
	add di, 01ah
	mov cx, word [es:di]
	push cx							; store FAT number
	add cx, ax
	add cx, DeltaSectorNo

	mov ax, BaseOfLoader
	mov es, ax
	mov bx, OffsetOfLoader
	mov ax, cx

LABEL_GOON_LOADING_FILE:
	push ax
	push bx
	mov ah, 0Eh
	mov al, '.'						;write '.'
	mov bl, 0Fh
	int 10h
	pop bx
	pop ax

	mov cl,1
	call ReadSector
	pop ax
	call GetFATEntry
	cmp ax,0FFFh
	jz LABEL_FILE_LOADED

	push ax
	mov dx, RootDirSectors
	add ax, dx
	add ax, DeltaSectorNo
	jmp LABEL_GOON_LOADING_FILE

LABEL_FILE_LOADED:
	mov dh, 1
	call PrintStr

	jmp BaseOfLoader:OffsetOfLoader

GetFATEntry:						; find the sector num in FAT who sector num = ax ,return in ax
	push es
	push bx
	push ax
	mov ax, BaseOfLoader
	sub ax, 0100h
	mov es, ax
	pop ax
	
	mov byte [bOdd], 0
	mov bx, 3
	mul bx							; dx:ax = ax * 3
	mov bx, 2
	div bx							; dx:ax / 2  ->  ax*2 + dx

	cmp dx, 0
	jz LABEL_EVEN

	mov byte [bOdd], 1

LABEL_EVEN:
	
	xor dx, dx
	mov bx, [BPB_BytsPerSec]
	div bx							; dx:ax / BPB_BytsPerSec -> ax the Sector no in FATEntry
									;							dx the offset in Sector

	push dx

	mov bx, 0						; ready write to es:bx where BaseOfLoader - 100 : 00
	add ax, SectorNoOfFAT1			; now we get the sectorNO in floppy(disk)

	mov cl, 2
	call ReadSector					; read 2 sector for the each side both in odd one

	pop dx
	add bx, dx
	mov ax, [es:bx]					; now we callback the offset
	cmp byte [bOdd], 1
	jnz LABEL_EVEN_2
	shr ax, 4

LABEL_EVEN_2:
	and ax, 0FFFh

LABEL_GET_FAT_ENTRY_OK:
	
	pop bx
	pop	es							; we done now, return
	ret 

	; read data in es:bx
	; set in ax for start Sector num and cl for times

ReadSector:
	push bp
	mov	bp, sp
	sub esp, 2

	mov byte [bp-2], cl
	push bx
	mov bl, [BPB_SecPerTrk]			; 1.4M disk SecPerTrk = 18
	div bl

	inc ah
	mov cl, ah						; cl : begin of the Sector num

	mov dh, al

	shr al, 1						
	mov ch ,al						; ch : num of Cylinder

	and dh, 1						; dh : num of disk head

	pop bx

	mov dl, [BS_DrvNum]				; dl : load int 13 init num --start

.GoOnReading:
	mov ah, 2						; ah = 02h
	mov al, byte[bp-2]				; al : nums of read

	int 13h							; Start read
	jc .GoOnReading					; if Error set CF = 1 so we again

	add esp, 2						; read data in es:bx
	pop bp
	ret



PrintStr:
	mov		ax, MessageLength
	mul		dh

	add		ax, BootMSG

	mov		bp, ax

	mov		ax, ds
	mov		es, ax

	mov		cx, MessageLength			; Length
	mov		ax, 01301h
	mov		bx, 000ch
	mov		dl, 0
	int		10h
	ret

wRootDirSizeForLoop	dw	RootDirSectors	; loop for rootdir
wSectorNo			dw	SectorNoOfRootDirectory				; ready read sector

bOdd				db	0				; odd or not

LoaderFileName		db	"LOADER  BIN", 0

MessageLength		equ 9

BootMSG				db	"Starting."	; 9 bytes
MSG1				db  "Get it..."
MSG2				db	"No Loader"

times	510-($-$$)	db	0
dw		0xaa55



